package com.jiuyin.dianjing.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.jiuyin.dianjing.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TeamPostReportAdapter extends BaseAdapter {

    private Context context;

    private List<String> listText;

    private Map<String, Boolean> states = new HashMap<>();

    public TeamPostReportAdapter(Context context, List<String> listText) {
        this.context = context;
        this.listText = listText;
    }

    @Override
    public int getCount() {
        return listText.size();
    }

    @Override
    public String getItem(int position) {
        return listText.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            //通过一个打气筒 inflate 可以把一个布局转换成一个view对象
            view = View.inflate(context, R.layout.item_team_post_report, null);
        } else {
            view = convertView;//复用历史缓存对象
        }
        //单选按钮的文字
        TextView radioText = view.findViewById(R.id.tv_radio_text);
        //单选按钮
        RadioButton radioButton = view.findViewById(R.id.rb_radio_button);
        radioText.setText(listText.get(position));
        radioButton.setOnClickListener(v -> {
            // 重置，确保最多只有一项被选中
            for (String key : states.keySet()) {
                states.put(key, false);
            }
            states.put(String.valueOf(position), true);
            TeamPostReportAdapter.this.notifyDataSetChanged();
        });
        boolean res;
        if (states.get(String.valueOf(position)) == null || states.get(String.valueOf(position)) == false) {
            res = false;
            states.put(String.valueOf(position), false);
        } else
            res = true;

        radioButton.setChecked(res);
        return view;
    }
}
